package org.spring.springboot.app.user.sysuser.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.user.sysuser.domain.vo.UserLoginReqVO;
import org.spring.springboot.app.user.sysuser.domain.vo.UserLoginResVO;
import org.spring.springboot.app.user.sysuser.domain.vo.UserRegistReqVO;
import org.spring.springboot.app.user.sysuser.service.ISysUserService;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(description = "用户登录注册相关接口",tags = ApiIndex.LoginController)
@RequestMapping(value = "/api/sysUser")
@RestController
public class LoginController {

    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation(value = "用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @Transactional
    public R<UserLoginResVO> login(
            HttpServletRequest request,
            @RequestBody UserLoginReqVO userLoginReqVO) {
        R result = new R();
        if (StringUtils.isBlank(userLoginReqVO.getLoginId()) || StringUtils.isBlank(userLoginReqVO.getPassword())) {
            result.setType(Type.ParamValidateFail);
            return result;
        }
        return sysUserService.login(userLoginReqVO,request);
    }

    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/regist", method = RequestMethod.POST)
    @Transactional
    public R<UserRegistReqVO> regist(
            HttpServletRequest request,
            @RequestBody UserRegistReqVO userRegistReqVO) {
        R result = new R();
        return sysUserService.regist(userRegistReqVO,request);
    }


}
